const { resolve } = require("path");
const minimist = require("minimist");
const { context } = require("esbuild");

const args = minimist(process.argv.slice(2)); // 解析用户执行命令行的参数

const { _, f } = args;
const target = _[0] || "reactivity";
const format = f || "global";

// 找到打包的文件
const pkg = require(resolve(__dirname, `../packages/${target}/package.json`));

// 打包的格式
const outputFormat = format.startsWith("global")
  ? "iife"
  : format === "cjs"
  ? "cjs"
  : "esm";

// 最后打包的文件名字以及存放位置
const outfile = resolve(
  __dirname,
  `../packages/${target}/dist/${target}.${format}.js`
);

context({
  // 打包入口文件
  entryPoints: [resolve(__dirname, `../packages/${target}/src/index.ts`)],
  outfile, // 打包出口文件
  bundle: true,
  sourcemap: true,
  format: outputFormat, // 打包格式
  globalName: pkg.buildOptions?.name, // 打包文件名称
  platform: format === "cjs" ? "node" : "browser", // 打包平台
}).then(async (res) => {
  res.watch();
});
